<script setup lang="ts">
import { ContentWrap } from '@/components/ContentWrap'
import { Search } from '@/components/Search'
import { ElButton, ElTag } from 'element-plus'
import { Table } from '@/components/Table'
// import { ucOrgListApi } from '@/api/uc/org'
import { dbOrgListApi, dbOrgexcelApi } from '@/api/forms/index'

import { useTable } from '@/hooks/web/useTableTest'
import { TableData } from '@/api/uc/org/types'
import { h, reactive, ref } from 'vue'

import { CrudSchema, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
// 获取字典
import { useCache } from '@/hooks/web/useCache'
const { wsCache } = useCache()
const dict: any = wsCache.get('dict')
// 字典通过value获取label
const getDictLabel = (type, value) => {
  let data = dict[type].find((item) => {
    return item.value == value
  })
  return data ? data.label : ''
}

const { register, tableObject, methods } = useTable<
  {
    total: number
    list: TableData[]
  },
  TableData
>({
  getListApi: dbOrgListApi,
  response: {
    data: 'data',
    count: 'count'
  }
})

const { setSearchParams } = methods

const crudSchemas = reactive<CrudSchema[]>([
  {
    field: 'org_id',
    label: '主键',
    fixed: 'left',
    width: 60,
    table: {
      show: true
    },
    form: {
      show: false
    },
    detail: {
      show: false
    }
  },
  {
    field: 'org_name',
    label: '组织名称',
    width: 100,
    search: {
      show: true
    }
  },
  {
    field: 'org_type',
    label: '组织类型',
    width: 100,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('org_type', cellValue)
    },
    form: {
      component: 'Select',
      componentProps: {
        options: dict.org_type
      }
    },
    search: {
      component: 'Select',
      componentProps: {
        options: dict.org_type
      },
      show: true
    }
  },
  {
    field: 'state',
    label: '审核状态',
    width: 100,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return getDictLabel('org_state', cellValue)
    },
    form: {
      show: false
    },
    search: {
      component: 'Select',
      componentProps: {
        options: dict.org_state
      },
      show: true
    }
  },
  {
    field: 'is_stop',
    label: '是否停用',
    width: 80,
    formatter: (_: Recordable, __: TableColumn, cellValue: number | string) => {
      return h(
        ElTag,
        {
          type: cellValue == '0' ? 'success' : cellValue == '1' ? 'warning' : 'danger'
        },
        () => (cellValue == '0' ? '启用' : cellValue == '1' ? '停用' : '未知')
      )
    },
    form: {
      component: 'Select',
      componentProps: {
        options: [
          {
            label: '启用',
            value: '0'
          },
          {
            label: '停用',
            value: '1'
          }
        ]
      }
    }
  },
  {
    field: 'business_license',
    label: '营业执照',
    table: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24
    }
  },
  {
    field: 'business_license_img',
    label: '营业执照照片地址',
    table: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      },
      component: 'Upload'
    },
    detail: {
      span: 24
    }
  },
  {
    field: 'identity_card',
    label: '身份证号',
    table: {
      show: false
    },
    form: {
      colProps: {
        span: 24
      }
    },
    detail: {
      span: 24
    }
  },
  {
    field: 'identity_card_front',
    label: '身份证正面照片',
    table: {
      show: false
    },
    form: {
      component: 'Upload',
      colProps: {
        span: 12
      }
    },
    detail: {
      span: 24
    }
  },
  {
    field: 'identity_card_back',
    label: '身份证反面照片',
    table: {
      show: false
    },
    form: {
      component: 'Upload',
      colProps: {
        span: 12
      }
    },
    detail: {
      span: 24
    }
  },
  {
    field: 'owner_user_name',
    label: '所有者',
    width: 80,
    form: {
      show: false
    }
  },
  {
    field: 'owner_sys_role_name',
    label: '所有者角色',
    width: 120,
    form: {
      show: false
    }
  },
  {
    field: 'create_user_name',
    label: '创建人',
    // width: 80,
    form: {
      show: false
    }
  },
  {
    field: 'create_time',
    label: '创建时间',
    width: 170,
    form: {
      show: false,
      component: 'DatePicker',
      componentProps: {
        disabled: true,
        type: 'datetime',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    }
  },
  {
    field: 'update_user_name',
    label: '修改人',
    // width: 80,
    form: {
      show: false
    }
  },
  {
    field: 'update_time',
    label: '修改时间',
    width: 170,
    form: {
      show: false,
      component: 'DatePicker',
      componentProps: {
        disabled: true,
        type: 'datetime',
        valueFormat: 'YYYY-MM-DD HH:mm:ss'
      }
    }
  }
])

const { allSchemas } = useCrudSchemas(crudSchemas)

const exportClick = async () => {
  // 弹出导出框
  dialogTableVisible.value = true
  // 获取查询条件
  let aaa: any = tableObject.paramsObj.params
  console.log(aaa)
  const { data }: any = await dbOrgexcelApi({
    org_name: aaa.org_name,
    org_type: aaa.org_type,
    state: aaa.state
  })
    .catch(() => {})
    .finally(() => {})
  if (data) {
    console.log(data, 'giao')

    console.log(data.count, Math.ceil(data.count / 10000))
    FormsNumber.value = Math.ceil(data.count / 10000)
    // window.open(res.data.url)
  }
  // ElMessage.warning('开发中，稍后查看')
}

// 下载次数
const FormsNumber = ref(0)

const dialogTableVisible = ref(false)

const Export = async (index) => {
  let aaa: any = tableObject.paramsObj.params
  let { data }: any = await dbOrgexcelApi({
    org_name: aaa.org_name,
    org_type: aaa.org_type,
    state: aaa.state,
    pageNo: index,
    pageSize: 10000
  })
    .catch(() => {})
    .finally(() => {
      // saveLoading.value = false
    })
  if (data) {
    window.open(data.url)
  }
}
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
    <div class="mb-10px w-[100%] flex justify-end" v-show="tableObject.paramsObj.params">
      <ElButton type="primary" @click="exportClick()"> 导出EXCEL </ElButton>
    </div>
    <Table
      v-show="tableObject.paramsObj.params"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      :columns="allSchemas.tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :border="true"
      :pagination="{
        total: tableObject.count
      }"
      @register="register"
      :selection="false"
    />
  </ContentWrap>

  <el-dialog v-model="dialogTableVisible" title="组织主档导出详情">
    <div style="margin: 20px" v-for="(item, index) in FormsNumber" :key="index">
      <span style="margin: 20px">组织主档 - {{ item }}</span>
      <ElButton type="primary" @click="Export(item)">导出</ElButton>
    </div>
  </el-dialog>
</template>
